'use strict';

const moment = require('moment');

module.exports = app => {
  const { Sequelize } = app;

  const Goods = app.model.define('Goods', {
    id: {
      allowNull: false,
      autoIncrement: true,
      primaryKey: true,
      type: Sequelize.INTEGER
    },
    number: {
      allowNull: false,
      type: Sequelize.INTEGER(3),
      defaultValue: 0
    },
    style: {
      allowNull: false,
      type: Sequelize.INTEGER(1)
    },
    status: {
      allowNull: false,
      type: Sequelize.INTEGER(1),
      defaultValue: 0
    },
    type: {
      allowNull: false,
      type: Sequelize.INTEGER(1),
      defaultValue: 0
    },
    nickname: {
      allowNull: false,
      type: Sequelize.STRING(255),
      defaultValue: ''
    },
    brief: {
      allowNull: false,
      type: Sequelize.STRING(255)
    },
    remark: {
      allowNull: false,
      type: Sequelize.STRING(255),
      defaultValue: ''
    },
    userId: {
      allowNull: false,
      type: Sequelize.INTEGER
    },
    sponsor: {
      allowNull: false,
      type: Sequelize.STRING(255),
      defaultValue: ''
    },
    pv: {
      allowNull: false,
      type: Sequelize.INTEGER(1),
      // async get() {
      //   const goodsId = this.getDataValue('id');
      //   const pv = await app.redis.get('goods').get(`goodsUsers_pv_${goodsId}`);
      //   return pv;
      // },
      defaultValue: 0
    },
    uv: {
      allowNull: false,
      type: Sequelize.INTEGER(1),
      // async get() {
      //   const goodsId = this.getDataValue('id');
      //   const pv = await app.redis.get('goods').get(`goodsUsers_uv_${goodsId}`);
      //   return pv;
      // },
      defaultValue: 0
    },
    appId: {
      allowNull: false,
      type: Sequelize.STRING(100),
      defaultValue: ''
    },
    endAt: {
      type: Sequelize.DATE,
      get() { return this.getDataValue('endAt') ? moment(this.getDataValue('endAt')).format('YYYY-MM-DD HH:mm') : '' }
    },
    createdAt: {
      allowNull: false,
      type: Sequelize.DATE,
      get() { return moment(this.getDataValue('createdAt')).format('YYYY-MM-DD HH:mm') }
    },
    updatedAt: {
      allowNull: false,
      type: Sequelize.DATE,
      get() { return moment(this.getDataValue('updatedAt')).format('YYYY-MM-DD HH:mm') }
    },
    deletedAt: {
      type: Sequelize.DATE,
    }
  }, {
    charset: 'utf8mb4',
    collate: 'utf8mb4_general_ci',
    comment: '商品表',
    indexes: [{
      fields: ['type']
    }, {
      fields: ['status']
    }],
  });

  // Goods.beforeCreate((user, options, fn) => {

  //   console.log(user, options, '123123123123123123123123123123123123123123123123123123123123')

  //   fn(null, user);
  //   return user;

  // });

  Goods.prototype.comparePassword = (candidatePassword, callback) => {
    // callback(null, candidatePassword);
    console.log(candidatePassword, callback, 'candidatePasswordcandidatePasswordcandidatePassword')
  };

  Goods.associate = models => {
    // 详情页查找关联广告
    Goods.hasOne(app.model.Banners, { foreignKey: 'goodsId' });

    Goods.hasMany(app.model.GoodsPrizes, {
      foreignKey: 'goodsId',
      sourceKey: 'id',
    });

    Goods.hasMany(app.model.GoodsFigures, {
      foreignKey: 'goodsId',
      sourceKey: 'id',
    });

    Goods.belongsToMany(app.model.Users, {
      through: 'UsersGoods',
      foreignKey: 'goodsId',
    });

    Goods.hasMany(app.model.IntegralOuts, {
      foreignKey: 'goodsId',
      sourceKey: 'id',
    });
  }

  return Goods;

};